home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 13.6 KB | 507 lines |
- package symantec.itools.awt;
-
-
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Color;
- import java.awt.FontMetrics;
- import java.awt.Font;
- import java.awt.Rectangle;
- import java.awt.Graphics;
-
- // 01/29/97 TWB Integrated changes from Windows
- // 01/29/97 TWB Integrated changes from Macintosh
-
- /**
- * Creates a text string, with a three-dimensional visual effect, that is
- * usually attached to an option, box, or button.
- * <p>
- * An application or applet can change the label text string, but a user
- * cannot edit it.
- * <p>
- * Note: For most components, labels are usually created by specifying text
- * for the Label property of that component.
- * <p>
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
- public class Label3D
- extends Canvas
- implements AlignStyle,
- BevelStyle
- {
- /**
- * Constant indicating a drawing margin of 0 pixels around the inside of
- * the border.
- */
- public static final int INDENT_ZERO = 0;
-
- /**
- * Constant indicating a drawing margin of 1 pixel around the inside of
- * the border.
- */
- public static final int INDENT_ONE = 1;
-
- /**
- * Constant indicating a drawing margin of 2 pixels around the inside of
- * the border.
- */
- public static final int INDENT_TWO = 2;
-
- /**
- * the label text.
- */
- protected String sLabel3D;
-
-
- private int alignStyle;
- private int bevelStyle;
- private Color color1;
- private Color color2;
- private Color textColor;
- private Color borderedColor;
- private FontMetrics fm;
- private int xTemp;
- private int yTemp;
- private int indent = INDENT_ZERO;
- private boolean bOsFlag = false;
-
-
- /**
- * Constructs a Label3D with black text, center aligned, raised border,
- * no text, and a zero border indent.
- */
- public Label3D()
- {
- this("", ALIGN_CENTERED, BEVEL_RAISED, Color.black, INDENT_ZERO);
- }
-
- /**
- * Constructs a Label3D with black text, azero border indent, and the
- * specified text alignment, and bevel style.
- * @param sText text of the Label3D
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- */
- public Label3D(String sText, int alignStyle, int bevelStyle)
- {
- this(sText, alignStyle, bevelStyle, Color.black, INDENT_ZERO);
- }
-
- /**
- * Constructs a Label3D with a zero border indent and the specified attributes.
- * @param sText text of the Label3D
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param color the label text color
- */
- public Label3D(String sText, int alignStyle, int bevelStyle, Color color)
- {
- this(sText, alignStyle, bevelStyle, color, INDENT_ZERO);
- }
-
- /**
- * Constructs a Label3D with black label text and the specified attributes.
- * @param sText text of the Label3D
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param indent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- */
- public Label3D(String sText, int alignStyle, int bevelStyle, int indent)
- {
- this(sText, alignStyle, bevelStyle, Color.black, indent);
- }
-
- /**
- * Constructs a Label3D with the specified attributes.
- * @param sText text of the Label3D
- * @param alignStyle text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @param bevelStyle bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @param color the label text color
- * @param indent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- */
- public Label3D(String sText, int alignStyle, int bevelStyle, Color color, int indent)
- {
- String sOs = System.getProperty("os.name");
-
- if (sOs.startsWith("S") || // SunOS, Solaris
- sOs.startsWith("OSF") ) // OSF
- {
- bOsFlag = true;
- setFont(new Font("Dialog", Font.PLAIN, 10));
- }
- else
- {
- bOsFlag = false;
- }
-
- sLabel3D = sText;
- textColor = color;
- borderedColor = Color.black;
-
- setBorderIndent(indent, false);
- setAlignStyle(alignStyle);
- setBevelStyle(bevelStyle);
- }
-
-
- //--------------------------------------------------
- // accessor methods
- //--------------------------------------------------
-
- /**
- * Sets the alignment style.
- * @param style text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #getAlignStyle
- */
- public void setAlignStyle(int style)
- {
- alignStyle = style;
- invalidate();
- }
-
- /**
- * Gets the current alignment style.
- * @return text alignment style: ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
- * @see #setAlignStyle
- */
- public int getAlignStyle()
- {
- return alignStyle;
- }
-
- /**
- * Sets the border style.
- * @param style border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see #getBevelStyle
- */
- public void setBevelStyle(int style)
- {
- bevelStyle = style;
-
- switch (style)
- {
- case BEVEL_LOWERED :
- {
- color1 = Color.black;
- color2 = Color.white;
- break;
- }
-
- case BEVEL_RAISED :
- {
- color1 = Color.white;
- color2 = Color.black;
- break;
- }
-
- case BEVEL_LINE :
- {
- color1 = borderedColor;
- color2 = borderedColor;
- break;
- }
-
- default: // catches any bogus type, paint(...) relies on color1 being null
- {
- color1 = color2 = null;
- break;
- }
- }
-
- invalidate();
- }
-
- /**
- * Gets the current border style.
- * @return border bevel style: BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE
- * @see #setBevelStyle
- */
- public int getBevelStyle()
- {
- return bevelStyle;
- }
-
- /**
- * Sets the border indent amount.
- * @param indent the amount to indent around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see #getBorderIndent
- */
- public void setBorderIndent(int indent)
- {
- setBorderIndent(indent, true);
- }
-
- /**
- * Gets the current border indent amount.
- * @return the amount currently indented around the border: INDENT_ZERO,
- * INDENT_ONE, or INDENT_TWO
- * @see #setBorderIndent
- */
- public int getBorderIndent()
- {
- return indent;
- }
-
- /**
- * Sets the border color.
- * @param color color to use for the border
- */
- public void setBorderedColor(Color color)
- {
- borderedColor = color;
-
- if (bevelStyle == BEVEL_LINE)
- {
- color1 = color;
- color2 = color;
- }
-
- invalidate();
- }
-
- /**
- * Sets the label text.
- * @param sLabel the new label text
- * @see #getText
- */
- public void setText(String sText)
- {
- sLabel3D = sText;
- repaint();
- }
-
- /**
- * Gets the current label text.
- * @return the current label text
- * @see #setText
- */
- public String getText()
- {
- return sLabel3D;
- }
-
- /**
- * Sets the label text color.
- * @param color the new color for the label text
- * @see #getTextColor
- */
- public void setTextColor(Color color)
- {
- textColor = color;
- invalidate();
- }
-
- /**
- * Gets the current label text color.
- * @return the current color of the label text
- * @see #setTextColor
- */
- public Color getTextColor()
- {
- return textColor;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(Graphics g)
- {
- Rectangle r;
- Color c;
-
- r = bounds();
- c = g.getColor();
-
- g.setColor(getBackground());
- g.fillRect(0, 0, r.width - 1, r.height - 1);
-
- if (color1 != null)
- {
- g.clipRect(0, 0, r.width, r.height);
-
- // top
- g.setColor(color1);
- g.drawLine(1+indent, indent, r.width-3-indent, indent);
-
- // bottom
- g.setColor(color2);
- g.drawLine(1+indent, r.height-1-indent, r.width-3-indent, r.height-1-indent);
-
- // left
- g.setColor(color1);
- g.drawLine(indent, indent, indent, r.height-1-indent);
-
- // right
- g.setColor(color2);
- g.drawLine(r.width-2-indent, indent, r.width-2-indent, r.height-1-indent);
-
- g.clipRect(2 + indent, 1+indent, r.width-9-indent, r.height-4-indent);
- yTemp = 1 + indent;
- }
- else
- {
- g.drawRect(indent, indent, r.width-2-indent, r.height-1-indent);
- g.clipRect(2, 1, r.width-7, r.height-2);
- yTemp = 1;
- }
-
- // text
- g.setColor(textColor);
-
- fm = getFontMetrics(getFont());
- yTemp = (r.height - yTemp + fm.getAscent()) / 2;
-
- switch (alignStyle)
- {
- case ALIGN_LEFT:
- {
- if (bevelStyle == BEVEL_LINE)
- {
- g.drawString(sLabel3D, 4, yTemp);
- }
- else
- {
- g.drawString(sLabel3D, 8, yTemp);
- }
-
- break;
- }
-
- case ALIGN_RIGHT:
- {
- xTemp = r.width - fm.stringWidth(sLabel3D);
-
- if (bevelStyle == BEVEL_LINE)
- {
- g.drawString(sLabel3D, xTemp - 6, yTemp);
- }
- else
- {
- g.drawString(sLabel3D, xTemp - 10, yTemp);
- }
-
- break;
- }
-
- case ALIGN_CENTERED:
- {
- xTemp = (r.width - fm.stringWidth(sLabel3D)) / 2;
- g.drawString(sLabel3D, xTemp, yTemp);
-
- break;
- }
- }
-
- // reset color
- g.setColor(c);
- }
-
- /**
- * Returns the recommended dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the recommended size of this component.
- *
- * @see #minimumSize
- */
- public Dimension preferredSize()
- {
- Dimension s = size();
- Dimension m = minimumSize();
-
- return new Dimension(Math.max(s.width, m.width), Math.max(s.height, m.height));
- }
-
- /**
- * Returns the minimum dimensions to properly display this component.
- * This is a standard Java AWT method which gets called to determine
- * the minimum size of this component.
- *
- * @see #preferredSize
- */
- public Dimension minimumSize()
- {
- Dimension min;
- Font f;
-
- min = new Dimension(18, 10);
- f = getFont();
-
- if (f == null)
- {
- if (bOsFlag)
- {
- min.height = 29;
- }
- }
- else
- {
- fm = getFontMetrics(f);
- min.width = fm.stringWidth(sLabel3D) + 18;
- min.height = fm.getHeight() + 10;
-
- if (bOsFlag && min.height < 29)
- {
- min.height = 29;
- }
- }
-
- return min;
- }
-
- /**
- * Moves and/or resizes this component.
- * This is a standard Java AWT method which gets called to move and/or
- * resize this component. Components that are in containers with layout
- * managers should not call this method, but rely on the layout manager
- * instead.
- *
- * @param x horizontal position in the parent's coordinate space
- * @param y vertical position in the parent's coordinate space
- * @param width the new width
- * @param height the new height
- */
- public synchronized void reshape(int x, int y, int width, int height)
- {
- super.reshape(x, y, width, height);
-
- if (!isValid())
- {
- repaint();
- }
- }
-
- private void setBorderIndent(int indent, boolean bRepaint)
- {
- if (indent < INDENT_ZERO)
- {
- this.indent = INDENT_ZERO;
- }
- else if (indent > INDENT_TWO)
- {
- this.indent = INDENT_TWO;
- }
- else
- {
- this.indent = indent;
- }
-
- if (bRepaint)
- {
- repaint();
- }
- }
- }
-